/**
 *  Copyright (C) 2008 Richard Hall
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the 
 *  GNU General Public License as published by the Free Software Foundation; either version 3 of the 
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with this program; if not, 
 *  see <http://www.gnu.org/licenses>.
 *
 *  Additional permission under GNU GPL version 3 section 7
 *
 *  If you modify this Program, or any covered work, by linking or combining it with [name of library] 
 *  (or a modified version of that library), containing parts covered by the terms of 
 *  [name of library's license], the licensors of this Program grant you additional permission to 
 *  convey the resulting work. {Corresponding Source for a non-source form of such a combination shall 
 *  include the source code for the parts of [name of library] used as well as that of the 
 *  covered work.}
 *
 * 
 */

package com.gpsmedia.uploadutils;

import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.*;
import java.net.*;
import com.google.gdata.client.photos.*;
import com.google.gdata.data.photos.UserFeed;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.Person;
import com.google.gdata.data.PlainTextConstruct;
import java.util.*;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.ExifTags;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.photos.impl.ExifTag;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.gdata.data.photos.PhotoFeed;

/**
 * This class is a wrapper class to make use of the functions that are required
 * from the Google Data api. It enables the upload of picture files to Google's Picasaweb and 
 * retrieves a URL for use later.
 * <p>
 * <p>
 * http://picasaweb.google.com
 * <p>
 * 
 * @author Richard Hall
 */
public class PicasaTools {

    private String feedURL;
    private String albumName;
    private Person author;
    private String newPath;
    private PicasawebService myService;

    /**
	 * Constructor for this class.
	 * 
	 * @param userName
	 *            The user name
	 * @param password
	 *            The password
	 * @throws AuthenticationException
	 */
    public PicasaTools(String userName, String password) throws AuthenticationException {
        myService = new PicasawebService("GPS2GoogleEarth");
        myService.setUserCredentials(userName, password);
        feedURL = "http://picasaweb.google.com/data/feed/api/user/" + userName;
        author = new Person(userName, null, userName);


    }


    /**
	 * Adds a new album
	 * 
	 * @param name
	 *            The name of the album
	 * @param description
	 *            The description of the album
	 */
    public void addAlbum(String name, String description) {
        try {
            URL postUrl = new URL(feedURL);

            AlbumEntry myEntry = new AlbumEntry();

            myEntry.setTitle(new PlainTextConstruct(name));
            myEntry.setDescription(new PlainTextConstruct(description));

            myEntry.getAuthors().add(author);



            // Send the request and receive the response:

            AlbumEntry myAlbum = myService.insert(postUrl, myEntry);
            albumName = myAlbum.getFeedLink().getHref();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (ServiceException se) {
            se.printStackTrace();
        }

    }

    /**
	 * Adds a picture to the album
	 * 
	 * @param path
	 *            The path to the pcture
	 * @param title
	 *            The title of the picture
	 * @param description
	 *            The description of the picture
	 * @param longitude
	 *            The longitude
	 * @param latitude
	 *            The latitude
	 * @param time
	 *            The date/time
	 */
    public void addPhoto(String path, String title, String description, double longitude,
            double latitude, Date time) {
        try {

            PhotoEntry myPhoto = new PhotoEntry();
            myPhoto.setTitle(new PlainTextConstruct(title));
            myPhoto.setDescription(new PlainTextConstruct(description));
            myPhoto.setClient("GPS2Google");

            myPhoto.setGeoLocation(latitude, longitude);
            MediaFileSource myMedia = new MediaFileSource(new File(path), "image/jpeg");
            myPhoto.setMediaSource(myMedia);
            myPhoto.setTimestamp(time);


            URL feed = new URL(albumName);
            PhotoEntry returnedPhoto = myService.insert(feed, myPhoto);

            myService.update(new URL(returnedPhoto.getEditLink().getHref()), myPhoto);


            MediaContent content = (MediaContent) returnedPhoto.getContent();
            newPath = content.getUri() + ".jpg";
        } catch (ServiceException ex) {
            Logger.getLogger(PicasaTools.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PicasaTools.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /**
	 * Returns the new uploaded path
	 * 
	 * @return The path to the uploaded file
	 */
    public String getNewPath() {
        return newPath;
    }

    public static void main(String args[]) {
        try {
            PicasaTools pt = new PicasaTools("richard.w.hall", "marius");

            String Path = "C:/Work/Final Year Project/Picture Test1/IMG_3555.JPG";
            pt.addPhoto(Path, "Test pic", "This is a test pic", -0.10352011666, 51.5432496, new Date());
        } catch (AuthenticationException ex) {
            Logger.getLogger(PicasaTools.class.getName()).log(Level.SEVERE, null, ex);
        }



    }
}